package com.kld.amaptset.ui.adaper;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kld.amaptset.R;
import com.kld.amaptset.model.entity.Meizi;
import com.kld.amaptset.ui.activity.ScrollingActivity;
import com.kld.amaptset.ui.widget.RatioImageView;
import com.kld.amaptset.util.DateUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by KELUNGDE on 2017/8/5.
 */

public class MeiziRecycViewAdaper extends RecyclerView.Adapter<MeiziRecycViewAdaper.MeiziHolder> {


    List<Meizi> datas;
    Context mContext;
    int lastMeizi;

    public MeiziRecycViewAdaper(Context mContext, List<Meizi> meiziDatas) {
        this.datas = meiziDatas;
        this.mContext = mContext;
    }


    @Override
    public MeiziHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_meizi, parent, false);
        return new MeiziHolder(view);
    }

    @Override
    public void onBindViewHolder(MeiziHolder holder, int position) {
        Meizi meizi=datas.get(position);
        holder.carview.setTag(meizi);

        int red= (int) (Math.random()*255);
        int green= (int) (Math.random()*255);
        int blue= (int) (Math.random()*255);
        holder.iv_meizi.setBackgroundColor(Color.argb(204,red,green,blue));
        if (meizi!=null){
            Glide.with(mContext)
                    .load(meizi.url)
                    .crossFade()
                    .into(holder.iv_meizi);

            holder.who.setText(meizi.who);
            holder.desc.setText(meizi.desc);
            if (meizi.publishedAt!=null){
                holder.time.setText(DateUtil.toDateTimeStr(meizi.publishedAt));
            }
        }
    }



    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MeiziHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_meizi)
        RatioImageView iv_meizi;
        @BindView(R.id.tv_who)
        TextView who;
        @BindView(R.id.tv_avatar)
        TextView avatar;
        @BindView(R.id.tv_desc)
        TextView desc;
        @BindView(R.id.tv_time)
        TextView time;
        View carview;
        public MeiziHolder(View itemView){
            super(itemView);
            carview=itemView;
            ButterKnife.bind(this, itemView);
            iv_meizi.setOriginalSize(300,150);
        }

        @OnClick(R.id.iv_meizi)
        void meiziClick(){

        }

        @OnClick(R.id.rl_gank)
        void  gankClick(){
            mContext.startActivity(new Intent(mContext, ScrollingActivity.class));
        }
    }
}
